package hibernate;

import java.util.Arrays;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import pojo.Productcatalog;
import pojo.Products;
import dao.ProductCatalogDAO;
@Repository("ProductCatalogDAO")
public class HibernateProductCatalogDAO implements ProductCatalogDAO {

	private SessionFactory sessionFactory;
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	@Transactional
	public List<Productcatalog> getProductCatalogList() {
		Query query = sessionFactory.getCurrentSession().createQuery("from Productcatalog");
		return query.list();
	}
	
	@Transactional
	public Productcatalog getProductCatalog(String productCatalogId) {
		Session session = sessionFactory.getCurrentSession();
        Productcatalog pc = null;
        String hql = String.format("from Productcatalog pc where pc.catalogid= %s", productCatalogId);
        Query query = session.createQuery(hql);
        List<Productcatalog> listpc = query.list();
        if(listpc.size() == 1)
            pc = listpc.get(0);
        return pc;
	}
	
	@Transactional
	public int pagecount(String productname, String productCatalogId,
			String[]brands ,String[]conditions,String rating,int productonpage,String minprice,String maxprice) {
		Session session = sessionFactory.getCurrentSession();
        String hql = String.format("from Products p where 1=1 ");
        if(productname.trim().equals("") == false)
            hql = hql + " and p.productname like '%"+productname+"%' ";
        if(productCatalogId.equals("-1")==false){
            hql = hql +" and p.productcatalog.catalogid="+productCatalogId;
        }
        if(brands != null)
        {
        	List<String>brand = Arrays.asList(brands);
             hql += " and (p.brand.brandname =";
             for(int i = 0; i< brand.size() - 1;i++)
             {
             	hql+="'"+brand.get(i)+"'"+" or p.brand.brandname =";
             }
             hql+= "'"+brand.get(brand.size()-1)+"'" +")";
        	
        }
        if(conditions != null)
        {
        	List<String>condition = Arrays.asList(conditions);
             hql += " and (p.condition.conditionname =";
             for(int i = 0; i< condition.size() - 1;i++)
             {
             	hql+="'"+condition.get(i)+"'"+" or p.condition.conditionname =";
             }
             hql+= "'"+condition.get(condition.size()-1)+"'" +")";
        	
        }
        if(rating.equals("-1")== false)
      	  hql = hql+ " and p.rating >"+rating;
         
        if(minprice.trim().equals("") == false)
            hql = hql + " and p.price >="+minprice;
        if(maxprice.trim().equals("") == false)
            hql = hql + " and p.price <=" +maxprice;
        
        Query query = session.createQuery(hql);
        List<Products> ds = query.list();
        int sl = ds.size();
        int s;
        if (productonpage != -1) {
            s = sl / productonpage;
            if (sl % productonpage != 0) {
                s++;
            }
        }
        else
        {
            s=1;
        }
        
        return s;
	}
	
	}


